﻿#include "tabwidget.h"

// TabWidget 构造函数
TabWidget::TabWidget(QWidget *parent) : QTabWidget(parent)
{
    // 设置TabBar为自定义的TabBar
    setTabBar(new TabBar(this));

    // 设置默认关闭按钮完全隐藏
    tabBar()->setStyleSheet(
        "QTabBar::close-button {"
        "    image: none;"  // 移除默认图标
        "    background: transparent;"  // 背景透明
        "    border: none;"  // 无边框
        "    width: 0px;"  // 宽度为 0
        "    height: 0px;"  // 高度为 0
        "}"
    );
}

// 设置圆角
void TabWidget::setTabCornerRadius(int radius)
{
    if (tabBar())
    {
        qobject_cast<TabBar*>(tabBar())->setCornerRadius(radius);
    }
}

// 设置背景颜色
void TabWidget::setBackgroundColor(const QColor &color)
{
    if (tabBar())
    {
        qobject_cast<TabBar*>(tabBar())->setBackgroundColor(color);
    }
}

// 设置字体颜色
void TabWidget::setTextColor(const QColor &color)
{
    if (tabBar())
    {
        qobject_cast<TabBar*>(tabBar())->setTextColor(color);
    }
}

// 设置关闭按钮颜色
void TabWidget::setCloseButtonColor(const QColor &color)
{
    if (tabBar())
    {
        qobject_cast<TabBar*>(tabBar())->setCloseButtonColor(color);
    }
}

// 设置是否显示关闭按钮
void TabWidget::setCloseButtonVisible(bool visible)
{
    if (tabBar())
    {
        qobject_cast<TabBar*>(tabBar())->setCloseButtonVisible(visible);
    }
}

// TabBar 构造函数
TabBar::TabBar(QWidget *parent) : QTabBar(parent), cornerRadius(5), backgroundColor(Qt::white), textColor(Qt::black), closeButtonColor(Qt::gray), hoveredCloseButtonIndex(-1), pressedCloseButtonIndex(-1), closeButtonVisible(true)
{
    setCursor(Qt::PointingHandCursor); // 设置鼠标悬停时的光标样式
    setDrawBase(false); // 不绘制底部基线
    setMovable(true); // 允许选项卡拖拽
    setTabsClosable(true); // 启用默认的关闭按钮（但设置为透明）
}

// 设置圆角
void TabBar::setCornerRadius(int radius)
{
    cornerRadius = radius;
    update();
}

// 设置背景颜色
void TabBar::setBackgroundColor(const QColor &color)
{
    backgroundColor = color;
    update();
}

// 设置字体颜色
void TabBar::setTextColor(const QColor &color)
{
    textColor = color;
    update();
}

// 设置关闭按钮颜色
void TabBar::setCloseButtonColor(const QColor &color)
{
    closeButtonColor = color;
    update();
}

// 设置是否显示关闭按钮
void TabBar::setCloseButtonVisible(bool visible)
{
    closeButtonVisible = visible;
    update();
}

// 绘制关闭按钮
void TabBar::drawCloseButton(QPainter *painter, const QRect &rect, bool hovered, bool pressed)
{
    if (!closeButtonVisible) return; // 如果不显示关闭按钮，直接返回

    painter->save();

    // 根据状态设置关闭按钮颜色
    QColor buttonColor = closeButtonColor;
    if (pressed)
    {
        buttonColor = closeButtonColor.darker(150); // 点击效果
    }
    else if (hovered)
    {
        buttonColor = closeButtonColor.darker(120); // 滑过效果
    }

    painter->setPen(buttonColor);
    painter->setFont(QFont("Arial", 10, QFont::Bold));
    painter->drawText(rect, Qt::AlignCenter, "✕"); // 使用 "✕" 作为关闭按钮

    painter->restore();
}

// 重写绘制事件
void TabBar::paintEvent(QPaintEvent *event)
{
    Q_UNUSED(event);

    QPainter painter(this);
    QStyleOptionTab opt;

    // 遍历所有的Tab
    for (int i = 0; i < count(); ++i)
    {
        initStyleOption(&opt, i);

        // 绘制Tab的背景
        QRect rect = opt.rect;
        QPainterPath path;
        path.addRoundedRect(rect, cornerRadius, cornerRadius);

        if (opt.state & QStyle::State_Selected)
        {
            // 选中状态的Tab背景
            painter.fillPath(path, backgroundColor.darker(110)); // 选中时颜色加深
        }
        else if (opt.state & QStyle::State_MouseOver)
        {
            // 鼠标悬停状态的Tab背景
            painter.fillPath(path, backgroundColor.lighter(120)); // 浅色背景
        }
        else
        {
            // 默认状态的Tab背景
            painter.fillPath(path, backgroundColor);
        }

        // 绘制Tab的图标
        if (!opt.icon.isNull())
        {
            QRect iconRect = rect;
            iconRect.setWidth(16);
            iconRect.setHeight(16);
            iconRect.moveLeft(rect.left() + 4);
            iconRect.moveTop(rect.center().y() - iconRect.height() / 2);
            opt.icon.paint(&painter, iconRect);
        }

        // 绘制Tab的文本
        painter.setPen(textColor);
        painter.setFont(font());
        QRect textRect = rect;
        if (!opt.icon.isNull())
        {
            textRect.setLeft(rect.left() + 24);
        }
        painter.drawText(textRect, Qt::AlignCenter, opt.text);

        // 绘制关闭按钮
        if (closeButtonVisible)
        {
            int buttonSize = 16;
            int padding = 4;
            QRect closeButtonRect = QRect(opt.rect.right() - buttonSize - padding, opt.rect.center().y() - buttonSize / 2, buttonSize, buttonSize);

            bool hovered = (hoveredCloseButtonIndex == i);
            bool pressed = (pressedCloseButtonIndex == i);
            drawCloseButton(&painter, closeButtonRect, hovered, pressed);
        }
    }
}

// 重写Tab的大小计算
QSize TabBar::tabSizeHint(int index) const
{
    QSize size = QTabBar::tabSizeHint(index);
    size.setWidth(size.width() + 20); // 增加Tab的宽度
    size.setHeight(size.height() + 10); // 增加Tab的高度
    return size;
}

// 重写鼠标按下事件
void TabBar::mousePressEvent(QMouseEvent *event)
{
    if (event->button() == Qt::LeftButton)
    {
        // 获取点击的Tab索引
        int index = tabAt(event->pos());
        if (index != -1)
        {
            // 检查是否点击了关闭按钮
            int buttonSize = 16;
            int padding = 4;
            QRect closeButtonRect = QRect(tabRect(index).right() - buttonSize - padding, tabRect(index).center().y() - buttonSize / 2, buttonSize, buttonSize);

            if (closeButtonRect.contains(event->pos()))
            {
                pressedCloseButtonIndex = index;
                update();
                emit tabCloseRequested(index); // 触发关闭按钮点击信号
            }
            else
            {
                setCurrentIndex(index); // 设置当前选中的Tab
            }
        }
    }
    QTabBar::mousePressEvent(event);
}

// 重写鼠标移动事件
void TabBar::mouseMoveEvent(QMouseEvent *event)
{
    // 检查鼠标是否滑过关闭按钮
    int index = tabAt(event->pos());
    if (index != -1)
    {
        int buttonSize = 16;
        int padding = 4;
        QRect closeButtonRect = QRect(tabRect(index).right() - buttonSize - padding, tabRect(index).center().y() - buttonSize / 2, buttonSize, buttonSize);

        if (closeButtonRect.contains(event->pos()))
        {
            hoveredCloseButtonIndex = index;
        }
        else
        {
            hoveredCloseButtonIndex = -1;
        }
    }
    else
    {
        hoveredCloseButtonIndex = -1;
    }
    update();
    QTabBar::mouseMoveEvent(event);
}

// 重写鼠标离开事件
void TabBar::leaveEvent(QEvent *event)
{
    hoveredCloseButtonIndex = -1;
    update();
    QTabBar::leaveEvent(event);
}
